package main

import (
	"context"
	"flag"
	"fmt"
	"runtime"
	"time"

	"gitee.com/zhangxinzx/redis-bench/internal/utils"
	"github.com/go-redis/redis/v8"
)

var (
	redisPort string = "6379"
	redisPwd  string = "none"
)

func init() {
	flag.StringVar(&redisPort, "p", "6379", "redis server port")
	flag.StringVar(&redisPwd, "a", "none", "redis server password")
	flag.Parse()
}

func main() {
	rOpts := &redis.Options{
		Addr:     "127.0.0.1:" + redisPort,
		PoolSize: 2 * runtime.NumCPU(),
	}
	if redisPwd != "none" {
		rOpts.Password = redisPwd
	}
	redisMemTestCase := utils.NewRedisMemTestCase(rOpts)
	defer redisMemTestCase.Shutdown()

	count := 10000
	testSizes := []int{10, 20, 50, 100, 200, 1000, 5000}
	for _, tsize := range testSizes {
		err := redisMemTestCase.WriteToRedis(context.Background(), tsize, count)
		if err != nil {
			fmt.Println(err)
			return
		}
		memPerKey, err := redisMemTestCase.CalcMemoryUsagePerKey(context.Background())
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Printf("Redis SET value size [ %d B ] count [ %d ] memory usage per key [ %.4f B ]\n",
			tsize, count, memPerKey)
		err = redisMemTestCase.Reset(context.Background())
		if err != nil {
			fmt.Println(err)
			return
		}
		time.Sleep(500 * time.Millisecond)
	}
}
